package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.controller.admin.SetmealController;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealAndDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealAndDishMapper setmealAndDishMapper;

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
    * */
    @Override
    @Transactional  // 开启事务
    public void update(SetmealDTO setmealDTO) {
        //先把套餐基本数据修改
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //设置禁止
        setmeal.setStatus(StatusConstant.DISABLE);
        setmealMapper.update(setmeal);
        //查看和套餐套餐菜品关系表  关系是多对多
        //先删除再添加
        setmealAndDishMapper.deleteSetmealDishBySetmealId(List.of(setmealDTO.getId()));
        //添加
        List<SetmealDish> dishes = setmealDTO.getSetmealDishes();
        if(dishes==null || dishes.size()==0) {
            return;
        }
        for (SetmealDish dish : dishes) {
            dish.setSetmealId(setmealDTO.getId());
        }
        setmealAndDishMapper.insertBatch(dishes);
    }

    /**
     * 分页查询套餐
     * @param setmealPageQueryDTO
     * @return
     * */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //调用pagehelper
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> setmeals =setmealMapper.pageQuery(setmealPageQueryDTO);
        List<SetmealVO> result = setmeals.getResult();
        return new PageResult(setmeals.getTotal(),result);
    }


    /**
     * 起售或停售套餐
     * @param status
     * @param id
     * */
    @Override
    public void startOrStop(Integer status, Long id) {
        //简单的数据更改推荐单独一个sql,不用封装成类
        //但是为了后面aop在拦截方法中获取参数
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 批量删除套餐
     * @param ids
     * */
    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
        //批量删除套餐
        setmealMapper.deleteBatch(ids);
        //批量删除套餐和菜品关系表
        setmealAndDishMapper.deleteSetmealDishBySetmealId(ids);
    }

    /**
     * 新增套餐
     * @param setmealDTO
     * */
    @Transactional
    @Override
    public void add(SetmealDTO setmealDTO) {
        //新增套餐要返回套餐id
        //封装套餐数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //设置禁止
        setmeal.setStatus(StatusConstant.DISABLE);
        setmealMapper.insert(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes!=null && setmealDTO.getSetmealDishes().size()>0) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealId);
            }
            setmealAndDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 根据套餐id查询套餐菜品id
     * @param id
     * @return
     * */
    @Override
    public SetmealDTO queryById(Long id) {
        //查询套餐数据并回显出菜品数据
        Setmeal setmeal=setmealMapper.queryById(id);
        SetmealDTO setmealDTO = new SetmealDTO();
        BeanUtils.copyProperties(setmeal,setmealDTO);
        //查询菜品数据
        setmealDTO.setSetmealDishes(setmealAndDishMapper.getBySetmealId(id));
        return setmealDTO;
    }

    /**
     * 根据分类id查询套餐
     * @param categoryId
     * @return
     * */
    @Override
    public List<Setmeal> queryByCategoryId(Integer categoryId) {
        log.info("查询分类：{}",categoryId);
        List<Setmeal> setmeals = setmealMapper.queryByCategoryId(categoryId);
        return setmeals;
    }

    /**
     * 根据套餐id查询菜品数据  (不用口味)
     * @param Setmealid
     * @return
     * */
    @Override
    public List<DishItemVO> queryDishById(Long Setmealid) {
        //多表查询
        log.info("查询菜品：{}",Setmealid);
        List<DishItemVO> dishVOList = setmealAndDishMapper.queryDishById(Setmealid);
        return dishVOList;
    }


}
